<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-5-11
 * Time: 9:11
 */

namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\common\model\ProjectCategory;
use think\Db;
use app\common\model\Project as Project_Model;
use app\common\model\ProjectRateChange as ProjectRateChange_Model;

class Project extends Admin
{

    //公告列表
    public function index(){
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = Project_Model::where($map)->order($order)->paginate();
        // 分页数据
        $page = $data_list->render();

        $btn_add = [
            'title' => '新增项目',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];

        $btn_edit = [
            'title' => '修改项目',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-info btn-sm',
            'href' => url('edit',['id'=>"__id__"]),
        ];

        $btn_change_rate = [
            'title' => '修改以往项目提成',
            'icon' => 'fa fa-fw fa-scribd',
            'class' => 'btn btn-info btn-sm',
            'href' => url('changeRate',['id'=>"__id__"]),
        ];


        //设置页面
        return ZBuilder::make('table')
            ->addColumn('id', 'ID', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('name', '位置名称', 'text')
            ->addColumn('price', '项目价格', 'text')
            ->addColumn('add_block_price', '加钟价', 'text')
            ->addColumn('add_block_time', '加钟时长', 'text')
            ->addColumn('normal_rate', '标准提成', 'text')
            ->addColumn('queue_block_rate', '排钟提成', 'text')
            ->addColumn('dot_block_rate', '点钟提成', 'text')
            ->addColumn('add_block_rate', '加钟提成', 'text')
            ->addColumn('overtime_queue_rate', '加班排钟提成', 'text')
            ->addColumn('overtime_dot_rate', '加班点钟提成', 'text')
            ->addColumn('overtime_add_block_rate', '加班加钟提成', 'text')
            ->addColumn('receiver_queue_rate', '接待排钟提成', 'text')
            ->addColumn('receiver_dot_rate', '接待点钟提成', 'text')
            ->addColumn('receiver_add_rate', '接待加钟提成', 'text')
            ->addColumn('receiver_call_rate', '接单call钟提成', 'text')
            ->addColumn('change_block_rate', '改钟提成', 'text')
            ->addColumn('right_button', '操作', 'btn')
            ->setTableName('qt_project')// 指定数据表名
            ->addTopButton('add', $btn_add)// 添加顶部按钮
            ->addRightButton('edit', $btn_edit)// 添加顶部按钮
            ->addRightButton('changeRate', $btn_change_rate)// 添加顶部按钮
            ->addTopButton('delete')
            ->addTopButton('disable')
            ->addTopButton('enable')
            ->setPrimaryKey('id')// 设置主键名为username
            ->setPageTitle('项目列表')
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch();
    }

    //添加项目
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            $result = $this->validate($data, 'Project');

            if ($result!=true) $this->error($result);

            $info = Project_Model::get(['name'=>$data['name']]);

            if ($info)$this->error('项目名已经存在');

            if ($page = Project_Model::create($data)) {

                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        } else {
            $list = ProjectCategory::column('id,name');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['select:7','project_category_id','请选择项目类型','',$list],
                        ['text:7', 'name','','','',['项目名称:']],
                        ['text:7','content','','','',['项目描述:','']],
                        ['text:7', 'price', '','','',['项目价格:']],
                        ['text:7','time','','','',['服务时长','分钟']],
                        ['text:7','down_block_time','','','',['提前下钟时长:','分钟']],
                        ['text:7','add_block_price','','','',['加钟价:','元',]],
                        ['text:7','add_block_time','','','',['加钟时长:','分钟']],
                        ['text:7','normal_rate','','','',['标准提成:','元']],
                        ['text:7','mp_shop_price','','','',['公众号商城价:','']],
                        ['text:7','queue_block_rate','','','',['排钟提成:','元']],
                        ['text:7','dot_block_rate','','','',['点钟提成:','元']],
                        ['text:7','add_block_rate','','','',['加钟提成:','元']],
                        ['text:7','call_block_rate','','','',['call钟提成:','元']],
                        ['text:7','overtime_queue_rate','','','',['加班排钟提成:','元']],
                        ['text:7','overtime_dot_rate','','','',['加班点钟提成:','元']],
                        ['text:7','overtime_add_block_rate','','','',['加班加钟提成:','元']],
                        ['text:7','receiver_queue_rate','','','',['接待排钟提成:','元']],
                        ['text:7','receiver_dot_rate','','','',['接待点钟提成:','元']],
                        ['text:7','receiver_add_rate','','','',['接待加钟提成:','元']],
                        ['text:7','receiver_call_rate','','','',['接待call钟提成:','元']],
                        ['text:7','recommend_rate','','','',['技师推荐提成:','元']],
                        ['text:7','change_block_price_diff','','','',['改钟设置：价格相差','算改钟']],
                        ['text:7','change_block_rate','','','',['改钟设置：改钟提成','算改钟']],

                        ['text:7','block_number','','','',['计算钟数:','个']],
                        ['radio:7','is_free_order','自由开单:','',[1=>'是',0=>'否']],
                        ['radio:7','is_store_recommend','店长推荐:','',[1=>'是',0=>'否']],
                        ['radio:7','is_user_show','客人端显示:','',[1=>'是',0=>'否']],
                        ['text:7', 'sort','','','',['排序号:']],
                        ['images','images','项目图片'],


                    ]
                )
                ->setPageTitle('添加项目')
                ->fetch();
        }
    }


    //编辑公告
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Project');
            if (true !== $result) $this->error($result);

            if (Project_Model::where('id',$id)->update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = Project_Model::get($id);
            $category_list = ProjectCategory::column('id,name');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7', 'sort','','','',['排序号:']],
                        ['text:7', 'name','','','',['项目名称:']],
                        ['text:7', 'price', '','','',['项目价格:']],
                        ['text:7','time','','','',['服务时长','分钟']],
                        ['text:7','down_block_time','','','',['提前下钟时长:','分钟']],
                        ['text:7','add_block_price','','','',['加钟价:','']],
                        ['text:7','add_block_time','','','',['加钟时长:','分钟']],
                        ['text:7','mp_shop_price','','','',['公众号商城价:','']],
                        ['text:7','queue_block_rate','','','',['排钟提成:','元']],
                        ['text:7','dot_block_rate','','','',['点钟提成:','元']],
                        ['text:7','add_block_rate','','','',['加钟提成:','元']],
                        ['text:7','call_block_rate','','','',['call钟提成:','元']],
                        ['text:7','overtime_queue_rate','','','',['加班排钟提成:','元']],
                        ['text:7','overtime_dot_rate','','','',['加班点钟提成:','元']],
                        ['text:7','overtime_add_block_rate','','','',['加班加钟提成:','元']],
                        ['text:7','receiver_queue_rate','','','',['接待排钟提成:','元']],
                        ['text:7','receiver_dot_rate','','','',['接待点钟提成:','元']],
                        ['text:7','receiver_add_rate','','','',['接待加钟提成:','元']],
                        ['text:7','receiver_call_rate','','','',['接待call钟提成:','元']],
                        ['text:7','recommend_rate','','','',['技师推荐提成:','元']],
                        ['text:7','change_block_price_diff','','','',['改钟设置：价格相差','算改钟']],
                        ['text:7','change_block_rate','','','',['改钟设置：改钟提成','算改钟']],
                        ['select:7','project_category_id','请选择项目类型','',$category_list],
                        ['text:7','block_number','','','',['计算钟数:','个']],
                        ['radio:7','is_free_order','自由开单:','',[1=>'是',0=>'否']],
                        ['radio:7','is_store_recommend','店长推荐:','',[1=>'是',0=>'否']],
                        ['radio:7','is_user_show','客人端显示:','',[1=>'是',0=>'否']],
                        ['text:7','content','','','',['项目描述:','']],
                        ['images','images','项目图片'],
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑项目')
                ->fetch();
        }
    }

    /**
     * 修改以往项目
     * @param null $id
     * @return mixed
     * @throws \think\Exception
     */
    public function changeRate($id=null)
    {

        $info = ProjectRateChange_Model::get(['project_id'=>$id]);



        $project = Project_Model::alias('p')
            ->leftJoin('dp_qt_project_category c','p.project_category_id=c.id')
            ->where('p.id',$id)
            ->field('p.name project_name,c.name category_name,p.price,c.id project_category_id,p.price,p.id')
            ->find();

        if ($this->request->isPost())
        {
            $data = $this->request->post();
            $arr = [
                'project_id'=>$project['id'],
                'project_name'=>$project['project_name'],
                'project_price'=>$project['price'],
                'project_category_id'=>$project['project_category_id'],
                'start'=>strtotime($data['start']),
                'end'=>strtotime($data['end']),
                'queue_block_rate'=>$data['queue_block_rate'],
                'dot_block_rate'=>$data['dot_block_rate'],
                'add_block_rate'=>$data['add_block_rate'],
                'call_block_rate'=>$data['call_block_rate'],
                'overtime_queue_rate'=>$data['overtime_queue_rate'],
                'overtime_dot_rate'=>$data['overtime_add_block_rate'],
                'overtime_add_block_rate'=>$data['overtime_add_block_rate'],
                'receiver_queue_rate'=>$data['receiver_queue_rate'],
                'receiver_dot_rate'=>$data['receiver_dot_rate'],
                'receiver_add_rate'=>$data['receiver_add_rate'],
                'receiver_call_rate'=>$data['receiver_call_rate'],
                'change_block_rate'=>$data['change_block_rate'],
            ];

            $result = $info?ProjectRateChange_Model::where(['project_id'=>$project['id']])->update($arr): ProjectRateChange_Model::create($arr);

           if ($result)
           {
               $this->success('操作成功','index');
           }else{
               $this->error('操作失败');
           }
        }
        return ZBuilder::make('form')
            ->addFormItems(
                [
                    ['hidden', 'id',$id],
                    ['static:4', 'project_name','项目名称','',$project['project_name']],
                    ['static:4', 'project_price', '价格','',$project['price']],
                    ['static:4','project_category_id','类型','',$project['category_name']],
                    ['daterange:4','start,end','日期：'],
                    ['text:4','queue_block_rate','','','',['排钟提成','元']],
                    ['text:4','dot_block_rate','','','',['点钟提成','元']],
                    ['text:4','add_block_rate','','','',['加钟提成','元']],
                    ['text:4','call_block_rate','','','',['call钟提成','元']],
                    ['text:4','overtime_queue_rate','','','',['加班排钟提成','元']],
                    ['text:4','overtime_dot_rate','','','',['加班点钟提成','元']],
                    ['text:4','overtime_add_block_rate','','','',['加班加钟提成','元']],
                    ['text:4','receiver_queue_rate','','','',['接待排钟提成','元']],
                    ['text:4','receiver_dot_rate','','','',['接待点钟提成','元']],
                    ['text:4','receiver_add_rate','','','',['接待加钟提成','元']],
                    ['text:4','receiver_call_rate','','','',['接待call钟提成','元']],
                    ['text:4','change_block_rate','','','',['改钟提成','元']],

                ]
            )
            ->setFormData($info)
            ->setPageTitle('编辑项目')
            ->fetch();
    }

}